package org.acghub.mtdb.core;

import java.util.Objects;

public final class LogKey {
    private final TBean bean;
    private final String varname;

    LogKey(TBean xbean, String varname) {
        this.bean = xbean;
        this.varname = varname;
    }

    static LogKey of(TBean bean, String varname) {
        return new LogKey(bean, varname);
    }

    public TBean getBean() {
        return bean;
    }

    public String getVarname() {
        return varname;
    }

    public Object getValue() {
        return TBeanReflections.getValue(bean, varname);
    }

    public void setValue(Object value) {
        TBeanReflections.setValue(bean, varname, value);
    }

    @Override
    public int hashCode() {
        return bean.getObjId().hashCode() ^ varname.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (Objects.isNull(o)) {
            return false;
        }
        if (!(o instanceof LogKey l)) {
            return false;
        }
        if (this == o) {
            return true;
        }
        return Objects.equals(bean.getObjId(), l.getBean().getObjId()) && Objects.equals(varname, l.varname);
    }

    @Override
    public String toString() {
        return bean.getClass().getSimpleName() + "." + varname;
    }
}
